<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Backend_Forum extends Controller_Backend {
	
	public function action_sections()
	{
		$view = new View('backend/forum.sections.index');
		
		$columns = ORM::factory('Forum_Section')->table_columns();
		$view->columns = $columns;
		
		$sections = ORM::factory('Forum_Section')->order_by('sort', 'asc')->find_all();
		$view->sections = $sections;
		
		$this->template->content = $view;
	}
	
	public function action_section_edit()
	{
		$view = new View('backend/forum.sections.edit');
		
		$id = Request::current()->param('par1');
		
		$columns = ORM::factory('Forum_Section')->table_columns();
		$view->columns = $columns;
		
		$section = ORM::factory('Forum_Section')->where('id', '=', $id)->find();
		$view->section = $section;
		
		if (isset($_POST['section']))
		{
			$section->values($_POST['section'])->save();
			
			if (isset($_POST['save_and_return']))
				Request::current()->redirect(Route::get('backend')->uri(array(
					'controller' => 'forum',
					'action' => 'sections'
				)));
			else
				Request::current()->redirect(Route::get('backend')->uri(array(
					'controller' => 'forum',
					'action' => 'section_edit',
					'par1' => $section->id
				)));
		}
		
		$this->template->content = $view;
	}
	
	public function action_section_delete()
	{
		$id = Request::current()->param('par1');
		
		$section = ORM::factory('Forum_Section')->where('id', '=', $id)->find();
		foreach ($section->cats->find_all() as $cat)
		{
			foreach ($cat->topics->find_all() as $topic)
			{
				foreach ($topic->messages->find_all() as $message)
				{
					$message->user->forum_total_messages = $message->user->forum_total_messages - 1;
					$message->user->save();
					$message->delete();
				}
				foreach ($topic->subscribes->find_all() as $subscribe)
				{
					$subscribe->delete();
				}
				foreach ($topic->views->find_all() as $topicview)
				{
					$topicview->delete();
				}
				$topic->delete();
			}
			$cat->delete();
		}
		$section->delete();
		
		Request::current()->redirect(Request::current()->referrer());
	}
	
	public function action_cats()
	{
		$view = new View('backend/forum.cats.index');
		
		$columns = ORM::factory('Forum_Cat')->table_columns();
		$view->columns = $columns;
		
		$sections = ORM::factory('Forum_Section')->order_by('sort', 'asc')->find_all();
		$view->sections = $sections;
		
		$cats = ORM::factory('Forum_Cat')->order_by('sort', 'asc')->find_all();
		$view->cats = $cats;
		
		$this->template->content = $view;
	}
	
	public function action_cat_edit()
	{
		$view = new View('backend/forum.cats.edit');
		
		$id = Request::current()->param('par1');
		
		$columns = ORM::factory('Forum_Cat')->table_columns();
		$view->columns = $columns;
		
		$cat = ORM::factory('Forum_Cat')->where('id', '=', $id)->find();
		$view->cat = $cat;
		
		if (isset($_POST['cat']))
		{
			$cat->values($_POST['cat'])->save();
			
			if (isset($_POST['save_and_return']))
				Request::current()->redirect(Route::get('backend')->uri(array(
					'controller' => 'forum',
					'action' => 'cats'
				)));
			else
				Request::current()->redirect(Route::get('backend')->uri(array(
					'controller' => 'forum',
					'action' => 'cat_edit',
					'par1' => $cat->id
				)));
		}
		
		$this->template->content = $view;
	}
	
	public function action_cat_delete()
	{
		$id = Request::current()->param('par1');
		
		$cat = ORM::factory('Forum_Cat')->where('id', '=', $id)->find();
		foreach ($cat->topics->find_all() as $topic)
		{
			foreach ($topic->messages->find_all() as $message)
			{
				$message->user->forum_total_messages = $message->user->forum_total_messages - 1;
				$message->user->save();
				$message->delete();
			}
			foreach ($topic->subscribes->find_all() as $subscribe)
			{
				$subscribe->delete();
			}
			foreach ($topic->views->find_all() as $topicview)
			{
				$topicview->delete();
			}
			$topic->delete();
		}
		$cat->delete();
		
		Request::current()->redirect(Request::current()->referrer());
	}
	
	public function action_topics()
	{
		$view = new View('backend/forum.topics.index');
		
		$par1 = Request::current()->param('par1');
		$par2 = Request::current()->param('par2');
		
		$columns = ORM::factory('Forum_Topic')->table_columns();
		$view->columns = $columns;
		
		$cats = ORM::factory('Forum_Cat')->join('forum_section', 'LEFT')->on('forum_cat.section_id', '=', 'forum_section.id')->order_by(DB::expr('CONCAT('.Database::instance()->table_prefix().'forum_section.sort, \'-\', '.Database::instance()->table_prefix().'forum_cat.sort)'), 'ASC')->find_all();
		$view->cats = $cats;
		
		$sections = ORM::factory('Forum_Section')->order_by('sort', 'asc')->find_all();
		$view->sections = $sections;
		
		$query = ORM::factory('Forum_Topic');
		if ($par1 == 'section')
			$query->where('section_id', 'IN', explode(':', $par2));
		elseif ($par1 == 'cat')
			$query->where('cat_id', 'IN', explode(':', $par2));
		$query->order_by('date', 'ASC');
		
		$pagination = Pagination::factory(array(
			'total_items' => $query->count_all(),
		))->route_params(array(
			'directory' => 'backend',
			'controller' => 'forum',
			'action' => 'topic',
			'par1' => Request::current()->param('par1'),
			'par2' => Request::current()->param('par2'),
			'page' => Request::current()->param('page'),
		));
		$view->pagination = $pagination;
		
		$topics = $query->limit($pagination->items_per_page)->offset($pagination->offset)->find_all();
		$view->topics = $topics;
		
		$this->template->content = $view;
	}
	
	public function action_topic_edit()
	{
		$view = new View('backend/forum.topics.edit');
		
		$id = Request::current()->param('par1');
		
		$columns = ORM::factory('Forum_Topic')->table_columns();
		$view->columns = $columns;
		
		$topic = ORM::factory('Forum_Topic')->where('id', '=', $id)->find();
		$view->topic = $topic;
		
		if (isset($_POST['topic']))
		{
			$topic->values($_POST['topic'])->save();
			
			if (isset($_POST['save_and_return']))
				Request::current()->redirect(Route::get('backend')->uri(array(
					'controller' => 'forum',
					'action' => 'topics'
				)));
			else
				Request::current()->redirect(Route::get('backend')->uri(array(
					'controller' => 'forum',
					'action' => 'topic_edit',
					'par1' => $topic->id
				)));
		}
		
		$this->template->content = $view;
	}
	
	public function action_topic_delete()
	{
		$id = Request::current()->param('par1');
		
		$topic = ORM::factory('Forum_Topic')->where('id', '=', $id)->find();
		foreach ($topic->messages->find_all() as $message)
		{
			$message->user->forum_total_messages = $message->user->forum_total_messages - 1;
			$message->user->save();
			$message->delete();
		}
		foreach ($topic->subscribes->find_all() as $subscribe)
		{
			$subscribe->delete();
		}
		foreach ($topic->views->find_all() as $topicview)
		{
			$topicview->delete();
		}
		$topic->delete();
		
		Request::current()->redirect(Request::current()->referrer());
	}
	
	public function action_topic_field_update()
	{
		$id = Request::current()->param('par1');
		$column = Request::current()->param('par2');
		$value = Request::current()->param('par3');
		
		$topic = ORM::factory('Forum_Topic')->where('id', '=', $id)->find();
		$topic->{$column} = $value;
		$topic->save();
		
		Request::current()->redirect(Request::current()->referrer());
	}
	
	public function action_messages()
	{
		$view = new View('backend/forum.messages.index');
		
		$par1 = Request::current()->param('par1');
		$par2 = Request::current()->param('par2');
		
		$columns = ORM::factory('Forum_Message')->table_columns();
		$view->columns = $columns;
		
		$cats = ORM::factory('Forum_Cat')->join('forum_section', 'LEFT')->on('forum_cat.section_id', '=', 'forum_section.id')->order_by(DB::expr('CONCAT('.Database::instance()->table_prefix().'forum_section.sort, \'-\', '.Database::instance()->table_prefix().'forum_cat.sort)'), 'ASC')->find_all();
		$view->cats = $cats;
		
		$sections = ORM::factory('Forum_Section')->order_by('sort', 'asc')->find_all();
		$view->sections = $sections;
		
		$topics = ORM::factory('Forum_Topic')->select('id', 'name')->order_by('name', 'ASC')->find_all();
		$view->topics = $topics;
		
		$query = ORM::factory('Forum_Message');
		if ($par1 == 'section')
			$query->where('section_id', 'IN', explode(':', $par2));
		elseif ($par1 == 'cat')
			$query->where('cat_id', 'IN', explode(':', $par2));
		elseif ($par1 == 'topic')
			$query->where('topic_id', 'IN', explode(':', $par2));
		$query->order_by('date', 'ASC');
		
		$pagination = Pagination::factory(array(
			'total_items' => $query->count_all(),
		))->route_params(array(
			'directory' => 'backend',
			'controller' => 'forum',
			'action' => 'messages',
			'par1' => Request::current()->param('par1'),
			'par2' => Request::current()->param('par2'),
			'page' => Request::current()->param('page'),
		));
		$view->pagination = $pagination;
		
		$messages = $query->limit($pagination->items_per_page)->offset($pagination->offset)->find_all();
		$view->messages = $messages;
		
		$this->template->content = $view;
	}
	
	public function action_message_edit()
	{
		$view = new View('backend/forum.messages.edit');
		
		$id = Request::current()->param('par1');
		
		$columns = ORM::factory('Forum_Message')->table_columns();
		$view->columns = $columns;
		
		$message = ORM::factory('Forum_Message')->where('id', '=', $id)->find();
		$view->message = $message;
		
		if (isset($_POST['message']))
		{
			$message->values($_POST['message'])->save();
			
			if (isset($_POST['save_and_return']))
				Request::current()->redirect(Route::get('backend')->uri(array(
					'controller' => 'forum',
					'action' => 'messages'
				)));
			else
				Request::current()->redirect(Route::get('backend')->uri(array(
					'controller' => 'forum',
					'action' => 'message_edit',
					'par1' => $message->id
				)));
		}
		
		$this->template->content = $view;
	}
	
	public function action_message_delete()
	{
		$id = Request::current()->param('par1');
		
		$message = ORM::factory('Forum_Message')->where('id', '=', $id)->find();
		$message->user->forum_total_messages = $message->user->forum_messages->count_all() - 1;
		$message->user->save();
		$message->delete();
		
		Request::current()->redirect(Request::current()->referrer());
	}
	
	public function action_message_field_update()
	{
		$id = Request::current()->param('par1');
		$column = Request::current()->param('par2');
		$value = Request::current()->param('par3');
		
		$topic = ORM::factory('Forum_Message')->where('id', '=', $id)->find();
		$topic->{$column} = $value;
		$topic->save();
		
		Request::current()->redirect(Request::current()->referrer());
	}
	
}